package com.shop.controller;


import com.eova.common.Easy;
import com.eova.common.base.BaseController;
import com.eova.common.utils.util.StringUtils;
import com.jfinal.aop.Clear;
import com.jfinal.core.paragetter.Para;
import com.jfinal.json.FastJson;
import com.jfinal.kit.LogKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.oa.AuthorityService;
import com.oa.HttpClientUtil;
import com.seeyon.client.CTPRestClient;
import com.seeyon.ctp.common.security.MessageEncoder;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020-6-29.
 */
@Clear
public class AssetController extends BaseController {

    public void login(@Para("loginName") String loginName,@Para("password") String password){
        try{
            MessageEncoder me = new MessageEncoder();
            String pwd =  me.encode(loginName, password);
            int count = Db.use("oa").queryInt("select count(*) from org_principal where login_name = ? and credential_value = ?",loginName,pwd);
            renderJson(count);
        }catch (Exception e){

        }

    }



    private static String getToken() {
        String getTokenUri =  "https://oa2019.lnc.edu.cn"+ "/seeyon/rest/token/zcgl/zc123456";
        String  responseJsonStr = HttpClientUtil.doGet(getTokenUri);
        HashMap map = FastJson.getJson().parse(responseJsonStr, HashMap.class);

        if (map != null && map.get("id") != null) {
            //return (String) map.get("id");
           return (String) map.get("id");
        }
        return "";
    }


    public void getAssetInfo(@Para("zcid") String zcid){
        String current_token = getToken();
         String assetUri = "https://oa2019.lnc.edu.cn/seeyon/rest/LnAffer/getZCDA?code="+zcid+"&token="+current_token;
         String  responseJsonStr = HttpClientUtil.doGet(assetUri);
         renderJson(responseJsonStr);
    }

    public void startForm(@Para("zcData") String zcPostData,@Para("oaLoginName") String oaLoginName){
        Record userDate = Db.use("oa").queryFirst("SELECT\n" +
                " b.id userid,\n" +
                " c.id departid\n" +
                "FROM\n" +
                "\torg_principal a\n" +
                "left join org_member b\n" +
                "on a.MEMBER_ID = b.id\n" +
                "left join org_unit c\n" +
                "on b.org_department_id = c.id\n" +
                "WHERE\n" +
                "\ta.login_name = ?",oaLoginName);
        Long formId = sendFormFlow(oaLoginName,installOaForm(zcPostData,userDate.get("userid"),userDate.get("departid")),"ZCPDSP");
        renderJson(formId);

    }


    /**
     * 发起表单资产审批申流程
     * @param senderLoginName 发起者
     * @param xmlData 表单xml数据
     * @param templateCode 模版编码
     * @return flowId 表单流程ID
     */
    public static Long sendFormFlow(String senderLoginName,String xmlData, String templateCode){
        if(AuthorityService.getInstence().authenticate()) {
            try {
                CTPRestClient client= AuthorityService.getInstence().getClient();
                //Map 传参方式
                //String xmlData = "{\"flowTitle\":\"测试开发发送\",\"CreatePersonId\":\"3050820698737492296\",\"attachmentList\":[],\"formContent\":{\"definitions\":[],\"formId\":\"8568529539890168728\",\"formName\":\"formmain_0391\",\"subordinateForms\":[],\"textType\":\"3\",\"typeName\":\"FormExport\",\"values\":[{\"displayName\":\"文本1\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"文本域1\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"客户姓名\",\"typeName\":\"ValueExport\",\"value\":\"u\",\"version\":\"1\"},{\"displayName\":\"购买项目\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"联系电话\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"购买日期\",\"typeName\":\"ValueExport\",\"value\":\"2019-11-26\",\"version\":\"1\"},{\"displayName\":\"退款原因\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"文本6\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"文本7\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"数字1\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"数字2\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"扣除佣金金额\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"文本8\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"已退款金额\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"扣除手续费金额\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"实退金额\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"退款方式\",\"typeName\":\"ValueExport\",\"value\":\"0\",\"version\":\"1\"},{\"displayName\":\"账户名\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"开户行\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"账号\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"转出方\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"服务专员确认\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"销售总监确认\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"客服部复核\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"市场中心审核\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"财务部复核\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"},{\"displayName\":\"备注\",\"typeName\":\"ValueExport\",\"value\":\"\",\"version\":\"1\"}],\"version\":\"1\"},\"flowState\":\"3\",\"flowId\":\"7536013574766413237\",\"projectId\":\"0\",\"flowCreateTime\":\"2019-11-27 10:15:50\"}";
                Map data = new HashMap() {
                    {
                        put("senderLoginName",senderLoginName);//发起人登录名
                        put("data", xmlData);
                    }
                };

                Long flowData = client.post("flow/" + templateCode ,data, Long.class);//创建成功以后对应的流程Id
                if(flowData!= null && !"".equals(flowData)) {
                    LogKit.info("发起表单流程成功！！");
                    LogKit.info("flowId:"+ flowData);
                    return flowData;
                } else {
                    LogKit.info("发起表单流程失败！！");
                }
            } catch(Exception e) {
                LogKit.error("发起表单 流程异常！！异常信息：" + e.getMessage());
                e.printStackTrace();
            }
        }else {
            LogKit.info("身份验证失败！！！");
        }
        return null;
    }


    public static String installOaForm(String zcdata,String userId,String departId){
            String zcData = zcdata;
           JSONArray json = JSONArray.fromObject(zcData);
           String formData = "<formExport version=\"2.0\">\n" +
                  "<summary id=\"8249868212273235687\" name=\"formmain_1181\"/>\n" +
                  "<definitions/>\n" +
                  "<values>\n" +
                  "<column name=\"单据编号\">\n" +
                  "<value><![CDATA[S000002]]></value>\n" +
                  "</column>\n" +
                  "<column name=\"提交日期\">\n" +
                  "<value><![CDATA[2020-11-12]]></value>\n" +
                  "</column>\n" +
                  "<column name=\"单位\">\n" +
                  "<value><![CDATA[5701572655301151264]]></value>\n" +
                  "</column>\n" +
                  "<column name=\"盘点人\">\n" +
                  "<value><![CDATA["+userId+"]]></value>\n" +
                  "</column>\n" +
                  "<column name=\"所属部门\">\n" +
                  "<value><![CDATA["+departId+"]]></value>\n" +
                  "</column>\n" +
                  "<column name=\"盘点周期\">\n" +
                  "<value><![CDATA[0]]></value>\n" +
                  "</column>\n" +
                  "<column name=\"审批意见\">\n" +
                  "<value/>\n" +
                  "</column>\n" +
                  "</values>\n" +
                  "<subForms>\n" +
                  "<subForm>\n" +
                  "<definitions>\n" +
                  "<column id=\"field0006\" type=\"4\" name=\"序号1\" isNullable=\"false\" length=\"20\"/>\n" +
                  "<column id=\"field0007\" type=\"0\" name=\"资产编码\" isNullable=\"false\" length=\"100\"/>\n" +
                  "<column id=\"field0008\" type=\"0\" name=\"资产名称\" isNullable=\"false\" length=\"100\"/>\n" +
                  "<column id=\"field0009\" type=\"0\" name=\"存放地点\" isNullable=\"false\" length=\"100\"/>\n" +
                  "<column id=\"field0010\" type=\"0\" name=\"使用部门\" isNullable=\"false\" length=\"100\"/>\n" +
                  "<column id=\"field0011\" type=\"0\" name=\"状态\" isNullable=\"false\" length=\"100\"/>\n" +
                  "<column id=\"field0006\" type=\"4\" name=\"序号1\" isNullable=\"false\" length=\"20\"/>\n" +
                  "<column id=\"field0007\" type=\"0\" name=\"资产编码\" isNullable=\"false\" length=\"100\"/>\n" +
                  "<column id=\"field0008\" type=\"0\" name=\"资产名称\" isNullable=\"false\" length=\"100\"/>\n" +
                  "<column id=\"field0009\" type=\"0\" name=\"存放地点\" isNullable=\"false\" length=\"100\"/>\n" +
                  "<column id=\"field0010\" type=\"0\" name=\"使用部门\" isNullable=\"false\" length=\"100\"/>\n" +
                  "<column id=\"field0011\" type=\"0\" name=\"状态\" isNullable=\"false\" length=\"100\"/>\n" +
                  "</definitions>\n" +
                  "<values>\n";

                for(int i = 0;i<json.size();i++){
                    int no = 1+1;
                    JSONObject zcJson = json.getJSONObject(i);
                    formData += "<row>\n" +
                            "<column name=\"序号1\">\n" +
                            "<value><![CDATA["+no+"]]></value>\n" +
                            "</column>\n" +
                            "<column name=\"资产编码\">\n" +
                            "<value><![CDATA["+zcJson.get("zccode")+"]]></value>\n" +
                            "</column>\n" +
                            "<column name=\"资产名称\">\n" +
                            "<value><![CDATA["+zcJson.get("zcname")+"]]></value>\n" +
                            "</column>\n" +
                            "<column name=\"存放地点\">\n" +
                            "<value><![CDATA["+zcJson.get("zcbm")+"]]></value>\n" +
                            "</column>\n" +
                            "<column name=\"使用部门\">\n" +
                            "<value><![CDATA[111]]></value>\n" +
                            "</column>\n" +
                            "<column name=\"状态\">\n" +
                            "<value><![CDATA[正常]]></value>\n" +
                            "</column>\n" +
                            "</row>\n";
                }
//        formData+= "<row>\n" +
//                  "<column name=\"序号1\">\n" +
//                  "<value><![CDATA[2]]></value>\n" +
//                  "</column>\n" +
//                  "<column name=\"资产编码\">\n" +
//                  "<value><![CDATA[222]]></value>\n" +
//                  "</column>\n" +
//                  "<column name=\"资产名称\">\n" +
//                  "<value><![CDATA[222]]></value>\n" +
//                  "</column>\n" +
//                  "<column name=\"存放地点\">\n" +
//                  "<value><![CDATA[222]]></value>\n" +
//                  "</column>\n" +
//                  "<column name=\"使用部门\">\n" +
//                  "<value><![CDATA[22]]></value>\n" +
//                  "</column>\n" +
//                  "<column name=\"状态\">\n" +
//                  "<value><![CDATA[222]]></value>\n" ;



           formData += "</values>\n" +
                  "</subForm>\n" +
                  "</subForms>\n" +
                  "</formExport>";
           LogKit.info("发起表单数据！！"+formData);
          return formData;
    }
}
